﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sealight.Core.Domain;
using Sealight.API.Framework;
using NHibernate.Criterion;
using NHibernate;
using NHibernate.Linq;

namespace Sealight.Data
{
   public class ShoppingCartDataRepository: NHibernateRepository<ShoppingCartItemEntity>, IShoppingCartDataRepository
    {
       public IList<ShoppingCartItemEntity> GetCartItemsByUser(Guid user_id)
       {
           IList<ShoppingCartItemEntity> result = null;
           try
           {
               IQueryable<ShoppingCartItemEntity> sp = UnitOfWork.CurrentSession.Query<ShoppingCartItemEntity>();

               sp = sp.Where(c => c.UserSession.User != null && c.UserSession.User.UserID == user_id);

               result = sp.ToList();
           }
           catch (ADOException ex)
           {
               //rethrow the exception
               throw;
           }

           return result;
       }
       public IList<ShoppingCartItemEntity> GetCartItemsByUserSession(Guid user_session_id)
       {
           IList<ShoppingCartItemEntity> result = null;
           try
           {
               result = UnitOfWork.CurrentSession.CreateCriteria<ShoppingCartItemEntity>()
                   .CreateAlias("UserSession", "us")
                   .Add(Expression.Eq("us.UserSessionID", user_session_id))
                   .List<ShoppingCartItemEntity>();
           }
           catch (ADOException ex)
           {
               //rethrow the exception
               throw;
           }

           return result;
       }
    }
}
